package com.example.minimusicdemo;

import adapter.play.main.Adapter_pagerplay;
import adjust_volume.ApplierChain;
import adjust_volume.DrawerOpener;
import android.app.ActionBar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.example.project22_editingmediaplayer.MainActivity;
import com.example.project22_editingmediaplayer.R;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;
import com.example.project22_editingmediaplayer.service.MediaService;

public class Main_play extends FragmentActivity implements DrawerOpener {
	ViewPager viewPager;
	Adapter_pagerplay adapter_pagerplay;
	public static TextView artisplay, songplay;
	// private LayoutInflater mInflater;
	SongObject song;
	
	
	
	// variable of control volume
	public static AudioManager audioManager=null;
 ApplierChain applierChaincontrol;
public static SeekBar seekBarvolume;
public static View viewcontrolvolume;
public static int flag= 0; 





	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {

				int index = bundle.getInt("index");
				int lastindex = bundle.getInt("lastindext");

				try {
					song = SongResource.listSong.get(index);
					(Fragment_playmain.audio_player_track).setText(song
							.get_name());
					(Fragment_playmain.audio_player_album_artist).setText(song
							.get_artist());
					MainActivity.namesong.setText(song.get_name());
					MainActivity.singer.setText(song.get_artist());

					
					SongResource.listSong.get(MainActivity.mService.getlastIndex()).set_isplay(false);
					SongResource.listSong.get(MainActivity.mService.getIndex()).set_isplay(true);
					Fragment_playlist.ad_playlist.notifyDataSetChanged();

					Fragment_playmain.updateProgressBar();
				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainplayaudio);
		viewPager = (ViewPager) findViewById(R.id.pager);
		adapter_pagerplay = new Adapter_pagerplay(getSupportFragmentManager());
		viewPager.setAdapter(adapter_pagerplay);
		viewPager.setCurrentItem(1);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		
		// control volume
		applierChaincontrol =ApplierChain.createFor(this, this);
		audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
		seekBarvolume=(SeekBar)findViewById(R.id.seeckbarvolume1);
		seekBarvolume.setMax(15);
		seekBarvolume.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
		seekBarvolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
						
			}
		});
		viewcontrolvolume=(View)findViewById(R.id.volume_control);
		viewcontrolvolume.setVisibility(View.GONE);
		
	

	}
	
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(broadcastReceiver);
		applierChaincontrol.disable();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		registerReceiver(broadcastReceiver, new IntentFilter(
				MediaService.NOTIFICATION));
		applierChaincontrol.enable();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return (true);
			
			
		}

		return (super.onOptionsItemSelected(item));
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent i=new Intent(this,MainActivity.class);
		
		finish();
	}

	@Override
	public void openDrawer(int gravity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void closeDrawer(int gravity) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isDrawerOpen(int gravity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getRotation() {
		// TODO Auto-generated method stub
		return 0;
	}
public void delay( int second){
	final int milisecond=second*1000;
	runOnUiThread(new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			final Handler handler=new Handler();
			handler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					
					
				}
			}, milisecond);
			
		}
	});
}
	
}
